#include"AbstractBox2DTest.h"
#include"LSYEasyWorld.h"
#include"Box2D\Box2D.h"
#include"GLES-Render.h"
#include"Tool.h"

USING_NS_CC;

AbstractBox2DTest * AbstractBox2DTest::create(const b2Vec2 & gravity)
{
	auto pRet = new AbstractBox2DTest;
	if (pRet&&pRet->init(gravity))
	{
		pRet->autorelease();
		return pRet;
	}
	else
	{
		delete pRet;
		pRet = nullptr;
		return nullptr;
	}
}

bool AbstractBox2DTest::init(const b2Vec2& gravity)
{
	if (!Node::init())
	{
		return false;
	}
	createBox2dApp(gravity);
	box2DAppReady();
	scheduleUpdate();
	return true;
}

void AbstractBox2DTest::createBox2dApp(const b2Vec2& gravity)
{
	pWorld = LSYEasyWorld::createWorld(gravity);
	pDebug = LSYEasyWorld::createDebug();
}

void AbstractBox2DTest::update(float dt)
{
	LSYEasyWorld::updateWorld();
}

void AbstractBox2DTest::box2DAppReady()
{
}

void AbstractBox2DTest::draw(cocos2d::Renderer * renderer, const cocos2d::Mat4 & transform, uint32_t flags)
{
	Node::draw(renderer, transform, flags);
	ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position);
	kmGLPushMatrix();
	pWorld->DrawDebugData();
	kmGLPopMatrix();
	CHECK_GL_ERROR_DEBUG();
}

void AbstractBox2DTest::onExit()
{
	REALSE_NULL(pWorld);
}
